//package com.origin.filestorage.operation.upload.product;
//
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.text.StrPool;
//import com.google.gson.Gson;
//import com.origin.common.util.HttpsUtils;
//import com.origin.filestorage.operation.upload.domain.FileSliceUpload;
//import com.origin.filestorage.operation.upload.domain.FileUploadResult;
//import com.origin.filestorage.operation.upload.request.QiwenMultipartFile;
//import com.qiniu.common.QiniuException;
//import com.qiniu.http.Response;
//import com.qiniu.storage.Configuration;
//import com.qiniu.storage.UploadManager;
//import com.qiniu.storage.model.DefaultPutRet;
//import com.qiniu.storage.persistent.FileRecorder;
//import com.qiniu.util.Auth;
//import com.origin.filestorage.config.QiniuyunConfig;
//import com.origin.filestorage.enums.StorageTypeEnum;
//import com.origin.filestorage.enums.FileUploadStateEnum;
//import com.origin.filestorage.exception.operation.UploadException;
//import com.origin.filestorage.operation.upload.FileUploader;
//import com.origin.filestorage.util.QiniuyunUtils;
//import com.origin.filestorage.util.RedisUtil;
//import com.origin.filestorage.util.UFOPUtils;
//import lombok.extern.slf4j.Slf4j;
//
//import javax.annotation.Resource;
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//
///**
// * @author origin
// */
//@Slf4j
//public class QiniuyunKodoUploader extends FileUploader {
//
//    private final QiniuyunConfig qiniuyunConfig;
//
//    @Resource
//    private RedisUtil redisUtil;
//
//    public QiniuyunKodoUploader(QiniuyunConfig qiniuyunConfig) {
//        this.qiniuyunConfig = qiniuyunConfig;
//    }
//
//    @Override
//    protected FileUploadResult doUpload(QiwenMultipartFile qiwenMultipartFile, FileSliceUpload uploadFile) {
//        FileUploadResult uploadFileResult = new FileUploadResult();
//        try {
//            String fileUrl = UFOPUtils.getUploadFileUrl(uploadFile.getIdentifier(), qiwenMultipartFile.getExtendName());
//            uploadFileResult.setFileUrl(fileUrl);
//
//            File tempFile = UFOPUtils.getTempFile(fileUrl);
//            byte[] fileData = qiwenMultipartFile.getUploadBytes();
//            writeByteDataToFile(fileData, tempFile, uploadFile);
//
//            File processFile = UFOPUtils.getProcessFile(fileUrl);
//            // 判断是否完成文件的传输并进行校验与重命名
//            boolean isComplete = checkUploadStatus(uploadFile, processFile);
//            if (isComplete) {
//                uploadFileResult.setStatus(FileUploadStateEnum.SUCCESS);
//                uploadFileResult.setFileUrl(fileUrl);
//
//                qiniuUpload(fileUrl, tempFile);
//                FileUtil.del(tempFile);
//
//                if (UFOPUtils.isImageFile(uploadFileResult.getExtendName())) {
//                    Auth auth = Auth.create(qiniuyunConfig.getKodo().getAccessKey(), qiniuyunConfig.getKodo().getSecretKey());
//                    String urlString = auth.privateDownloadUrl(qiniuyunConfig.getKodo().getDomain().concat(StrPool.SLASH).concat(uploadFileResult.getFileUrl()));
//                    BufferedImage imageSrc;
//                    try (InputStream inputStream = HttpsUtils.doGet(urlString, null);) {
//                        imageSrc = ImageIO.read(inputStream);
//                        uploadFileResult.setBufferedImage(imageSrc);
//                    } catch (IOException e) {
//
//                    }
//                }
//            } else {
//                uploadFileResult.setStatus(FileUploadStateEnum.UNCOMPLETE);
//            }
//        } catch (IOException e) {
//            throw new UploadException(e);
//        }
//
//        uploadFileResult.setIdentifier(uploadFile.getIdentifier());
//        uploadFileResult.setFileName(qiwenMultipartFile.getFileName());
//        uploadFileResult.setExtendName(qiwenMultipartFile.getExtendName());
//        uploadFileResult.setFileSize(uploadFile.getTotalChunks() == 1 ? qiwenMultipartFile.getSize() : uploadFile.getTotalSize());
//        uploadFileResult.setStorageType(StorageTypeEnum.QINIUYUN_KODO);
//        return uploadFileResult;
//    }
//
//    @Override
//    public void cancelUpload(FileSliceUpload uploadFile) {
//    }
//
//    @Override
//    protected FileUploadResult organizationalResults(QiwenMultipartFile qiwenMultipartFile, FileSliceUpload uploadFile) {
//        return null;
//    }
//
//
//    private void qiniuUpload(String fileUrl, File file) {
//        Configuration qiniuConfig = QiniuyunUtils.getCfg(qiniuyunConfig);
//        // 指定分片上传版本
//        qiniuConfig.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
//        // 设置分片上传并发, 1：采用同步上传；大于 1：采用并发上传
//        qiniuConfig.resumableUploadMaxConcurrentTaskCount = 2;
//
//        Auth auth = Auth.create(qiniuyunConfig.getKodo().getAccessKey(), qiniuyunConfig.getKodo().getSecretKey());
//        String upToken = auth.uploadToken(qiniuyunConfig.getKodo().getBucketName());
//
//        String localTempDir = UFOPUtils.getStaticPath() + "temp";
//        try {
//            // 设置断点续传文件进度保存目录
//            FileRecorder fileRecorder = new FileRecorder(localTempDir);
//            UploadManager uploadManager = new UploadManager(qiniuConfig, fileRecorder);
//            Response response = uploadManager.put(file.getAbsoluteFile(), fileUrl, upToken);
//            // 解析上传成功的结果
//            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            log.info("key: {}, hash: {}", putRet.key, putRet.hash);
//        } catch (QiniuException ex) {
//            Response r = ex.response;
//            System.err.println(r.toString());
//            try {
//                System.err.println(r.bodyString());
//            } catch (QiniuException ex2) {
//                // ignore
//            }
//        } catch (IOException e) {
//        }
//    }
//
//}
